<?php 
include("util.php");
include("header.php");
$count_per_page = 50;
$page = intval($_GET['page']);
if ($page == null || $page < 1) {
  $page = 1;
}

$category = intval($_GET['cat']);
if ($category == null) {
  $category = 0;
}

$conn = mysql_connect("127.0.0.1","root","");
mysql_select_db("xingtan");

$sql=sprintf("SELECT count(a.id) AS total FROM articles AS a, authors AS u WHERE a.author = u.id AND a.category=%d", $category);
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$total_pages = ceil($row['total'] / $count_per_page);

$sql=sprintf("SELECT a.id AS a_id, a.title AS a_title, a.digest AS a_digest, a.create_time AS a_create_time, 
    u.id AS u_id, u.name AS u_name, u.title AS u_title, u.url AS u_url 
    FROM articles AS a, authors AS u WHERE a.author = u.id AND a.category=%d ORDER BY a.id DESC limit %d", 
    $category, $page * $count_per_page);
$result = mysql_query($sql);
?>

<div class="container">
	<div class="main">
		<div class="content">
			<div class="articles">
        <div style="margin:4px 0 6px;font-size:16px;font-weight:bold;"><a href="/page/0/">全部文章</a> 
          <?php 
            if (!($category_map = apc_fetch('category-map'))) {
              $category_map = array();
              $sql=sprintf("SELECT id, name, title FROM categories;");
              $cat_result = mysql_query($sql);
              while($row = mysql_fetch_array($cat_result)) {
                $category_map[$row['id']] = $row;
              }
              apc_add('category-map', $category_map, 3600);
            }
            printf(' &gt; <a href="/cat%d/">%s</a>', $category, $category_map[$category]['name']);
          ?> 
        </div>
        <?php 
        CommonPageNav($total_pages, $count_per_page, $page, 3, sprintf("/cat%d-%%d/", $category));
        print('<div class="article_body">');
        $index = 0;
        while($row = mysql_fetch_array($result)) {
          if ($index >= ($page - 1) * $count_per_page) {
            ArticlePreview($row['a_id']);
          }
          ++$index;
        }
        print('</div>');
        CommonPageNav($total_pages, $count_per_page, $page, 3, sprintf("/cat%d-%%d/", $category));
        ?>
		</div>
		</div>
	</div>
</div>
<?php include("back-top.php"); ?>
<?php 
mysql_close($conn);
include("footer.php");
?>
